package com.liao.ten.article;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.google.gson.Gson;
import com.liao.ten.R;
import com.liao.ten.base.BaseFragment;
import com.liao.ten.utils.ContentUtils;
import com.liao.ten.utils.FileUtil;

import java.util.ArrayList;
import java.util.List;

public class ArticleFragment extends BaseFragment implements ViewPager.OnPageChangeListener {

    private final int DATA1 = 1;
    private final int DATA2 = 2;

    private String path = "";

    private ViewPager fragVp;
    private ImageView top_iv_icon;
    private ImageView top_tv_data1;
    private ImageView top_tv_data2;
    private ImageView top_tv_week;
    private ImageView top_tv_divider;
    private ImageView top_tv_month;
    private Context context;

    private List<ArticleInfo.ResultBean> resultBeans;
    private List<ArticleDetailInfo> articleDetailInfos;
    private List<Fragment> fragments;
    private ArticleFragmentPagerAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_style, null);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        if (isNetworkAvailable(context)) {
            showDialog(context);
            loadData(context, ContentUtils.ARTICLE_URL, DATA1, "article.txt");
        } else {
            showData(FileUtil.getDataToFile(context, "article.txt"), DATA1);
        }
        initData();
    }

    private void initData() {
        top_iv_icon.setImageResource(R.mipmap.logo_novel);
    }

    private void initView(View view) {
        fragVp = (ViewPager) view.findViewById(R.id.frag_vp);
        top_iv_icon = view.findViewById(R.id.top_iv_icon);
        top_tv_data1 = view.findViewById(R.id.top_tv_data1);
        top_tv_data2 = view.findViewById(R.id.top_tv_data2);
        top_tv_week = view.findViewById(R.id.top_tv_week);
        top_tv_divider = view.findViewById(R.id.top_tv_divider);
        top_tv_month = view.findViewById(R.id.top_tv_month);

        fragVp.addOnPageChangeListener(this);
        context = getContext();

        resultBeans = new ArrayList<>();
        articleDetailInfos = new ArrayList<>();
        fragments = new ArrayList<>();
        adapter = new ArticleFragmentPagerAdapter(getChildFragmentManager(), fragments);
        fragVp.setAdapter(adapter);
    }

    @Override
    public void showDataFromNet(String s, int id) {
        showData(s, id);
    }

    private void showData(String s, int id) {
        if (id == DATA1) {
            ArticleInfo articleInfo = new Gson().fromJson(s, ArticleInfo.class);
            for (ArticleInfo.ResultBean resultBean : articleInfo.getResult()) {
                int beanId = resultBean.getId();
                resultBeans.add(resultBean);
                if (isNetworkAvailable(context)) {
                    loadData(context, ContentUtils.ARTICLE_DETATLS_URL + beanId, DATA2, 2);
                } else {
                    showData1(FileUtil.getDataToDB("articleId", "articledetailinfo", ArticleDetailInfo.class, beanId));
                }
            }
        } else if (id == DATA2) {
            showData1(s);
        }
    }

    private void showData1(String s) {
        ArticleDetailInfo articleDetailInfo = new Gson().fromJson(s, ArticleDetailInfo.class);
        articleDetailInfos.add(articleDetailInfo);
        if (articleDetailInfos.size() == 10) {
            for (int i = 0; i < articleDetailInfos.size(); i++) {
                ItemArticleLayoutFragment itemArticleLayoutFragment = new ItemArticleLayoutFragment();
                Bundle bundle = new Bundle();
                bundle.putSerializable("data1", resultBeans.get(i));
                bundle.putSerializable("data2", articleDetailInfos.get(i));
                itemArticleLayoutFragment.setArguments(bundle);
                fragments.add(itemArticleLayoutFragment);
            }
            dismissDialog();
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
